﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class admin_Modules_News_AddNew : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
       this.btnAddNew.Attributes.Add("onclick", "return OnSubmitClick();");
        this.btnCancel.Attributes.Add("onclick","return ClearAll()");

        if (!this.IsPostBack) {
            ViewState["URLReferrer"] = Request.UrlReferrer.AbsoluteUri;
            LoadCategory();
        }
    }

    

    private void LoadCategory() {
        WebData db = new WebData();
        this.cbbidCat.DataSource = db.tbl_Downloads.GetDataTable("select * from tbl_TrangThai order by orderId",
            CommandType.Text);
        this.cbbidCat.DataBind();   
        
    }

    private void AddNew() {
        String strMess = "";
        int id = 0;
            try
            {
                Dami.clsFileUpLoad fileUpload = new Dami.clsFileUpLoad();
                WebData db = new WebData();
                DataLibrary.tbl_News  news= new DataLibrary.tbl_News();
                news.IdTrangThai = Convert.ToInt32(cbbidCat.SelectedValue);
                news.Description =txtTieude.Text;
                news.IdCat = 2;
                news.AddedDate = DateTime.Now;
                news.ImageNew = fileUpload.FileSaveImage("upload/news",this.FileUpload1);
                news.IsActive = 1;
                news.IsAuto = false;
                news.OrderId = Convert.ToInt32(txtorderId.Text);
                id= db.tbl_Newss.InsertOnSubmit(news);

                news = db.tbl_Newss.GetAll().FindAll(p=>p.IsAuto==false)[0];
                id = news.IdNew;

                DataLibrary.tbl_News_Lang item = new DataLibrary.tbl_News_Lang();
                item.IdLang = 1;
                item.IdNew = id;
                item.IntroNew = txtDescription.Text;
                item.TitleNew = txtTieude.Text;
                item.BodyNew = txtNoidung.Text;
                
                id= db.tbl_News_Langs.InsertOnSubmit(item);


            }
            catch (Exception err)
            {
                strMess = Message.Insert_err + "! vì lỗi " + err.Message.ToString();
            }
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + strMess + "');", true);
       if(id!=-1)
           Response.Redirect(ViewState["URLReferrer"].ToString());

    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        AddNew();
    }

}